package com.acme.bankapp.domine.bank;

import com.acme.bankapp.service.bank.ClientRegistrationListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// [MY] Constructor Bank(ClientRegistrationListener[] listeners) is not implemented. (see p.7)
// [PE] done
public class Bank {
    private int countClients;
    private final int MAX_COUNT_CLIENTS = 10;
    private Client[] clients;
    // [MY] This field must be initialized (ex. listeners = new ArrayList()). You have java.lang.NullPointerException
    // [PE] I didn't want to send lab to review. Left casually.
    private List<ClientRegistrationListener> listeners;

    public Bank(List<ClientRegistrationListener> listeners) {
        clients = new Client[MAX_COUNT_CLIENTS];
        this.listeners = listeners;
        addListeners(new DebugListener());
    }

    public static class PrintClientListener implements ClientRegistrationListener {
        @Override
        public void onClientAdded(Client client) {
            System.out.println(client.getClientSalutation());
        }
    }

    public static class EmailNotificationListener implements ClientRegistrationListener {
        @Override
        public void onClientAdded(Client client) {
            System.out.println("Notification email for client %s to be sent");
        }
    }

    public static class DebugListener implements ClientRegistrationListener {
        @Override
        public void onClientAdded(Client client) {
            System.out.println(client.getClientSalutation() + " " + new Date());
        }
    }

    public void addListeners(ClientRegistrationListener clientRegistrationListener) {
        listeners.add(clientRegistrationListener);
    }

    public int getCountClients() {
        return countClients;
    }

    public Bank() {
        clients = new Client[MAX_COUNT_CLIENTS];
        listeners = new ArrayList<ClientRegistrationListener>();
        addListeners(new DebugListener());
    }

    public Client[] getClients() {
        return clients;
    }

    public void addClient(Client client) {
        if (countClients < MAX_COUNT_CLIENTS)
        {
            clients[countClients++] = client;
            for (ClientRegistrationListener clientRegistrationListener: listeners) {
                clientRegistrationListener.onClientAdded(client);
            }
        }
        else
            System.out.println("Storage of clients filled completely");
    }
}